package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/app/middleware"
	"zimuzu_cvn_web_api/service"
)

type MessageAPI struct {
}

func (message MessageAPI) Name() string {
	return "index API"
}

func (message MessageAPI) Bind() {
	messageAPI := config.C.Router.Group("/v1/app/message", middleware.TrackLog(), middleware.AuthMiddleware())
	{
		messageAPI.GET("check", msgCheck)
		messageAPI.GET("dealList", dealList)
		messageAPI.GET("noticeList", noticeList)
		messageAPI.GET("noticeDetail", noticeDetail)
	}
}

//查询未读消息条数
//Auth: Xu
func msgCheck(c *gin.Context) {
	uid, _ := service.GetUserIdByRequest(c)
	dealUnreadNum, otherDealUnreadNum, noticeUnreadNum, err := service.GetUnreadMessageNum(uid)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}

	responseData := map[string]int{
		"dealMsgNum":   dealUnreadNum,
		"noticeMsgNum": noticeUnreadNum,
		"otherMsgNum":  otherDealUnreadNum,
	}
	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, responseData)
	return
}

type dealListParams struct {
	model.PageParams
	Type int `form:"type" json:"type"` //0cvnt  1其他
}

//转账消息列表
//Auth: Xu
func dealList(c *gin.Context) {
	var params dealListParams
	params.PageParams = model.NewDefaultPageParams()
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	uid, _ := service.GetUserIdByRequest(c)
	list, err := service.GetDealMessageList(uid, params.Per, params.Page, params.Type)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, list)
	return
}

type noticeListParams struct {
	Page int `form:"page" json:"page"`
	Per  int `form:"per" json:"per"`
}

//系统消息列表
//Auth： Xu
func noticeList(c *gin.Context) {
	var params noticeListParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	uid, _ := service.GetUserIdByRequest(c)
	announcementList, err := service.GetAppAnnouncement(uid, params.Page, params.Per)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, announcementList)
	return
}

type noticeDetailParams struct {
	ID int `form:"id" json:"id" binding:"required"`
}

//系统消息详情
//Auth: Xu
func noticeDetail(c *gin.Context) {
	var params noticeDetailParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	detail, err := service.GetNoticeMessageNotice(params.ID)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, detail)
	return
}
